package Modele;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;

public class Representation extends Model
{
	/**
	 * VARIABLES DE LA CLASSE 
	 */
	private int IdRepresentation; 
	private Spectacle spectacle;
	private Emplacement emplacement;
	private Calendar date;
	private int nbBillets;
	private int nbBilletsAchetes = 0;
	private int nbBilletsReserves = 0;
	private int cout;
	
	private String titre;
	private ArrayList<Billet> billets = new ArrayList<Billet>();
	private Description description;
	
	public Representation( int id, Spectacle spectacle, Emplacement emplacement, Calendar date, int nbBillets, int cout, String titre, Description desciprition)
	{
		setIdRepresentation ( id );
		this.spectacle = spectacle;
		this.emplacement = emplacement;
		this.date = date;
		this.nbBillets = nbBillets;
		this.cout = cout;	
		this.titre=titre;
		this.description=desciprition;
	}
	

	
	public int getIdRepresentation() {
		return getId();
	}



	public void setIdRepresentation(int idRepresentation) {
		setId(idRepresentation);
	}

	
	public Description getDescription() {
		return this.description;
	}



	public void setDescription(Description description) {
		this.description = description;
	}

	public Spectacle getSpectacle() {
		return spectacle;
	}



	public void setSpectacle(Spectacle spectacle) {
		this.spectacle = spectacle;
	}



	public Emplacement getEmplacement() {
		return emplacement;
	}



	public void setEmplacement(Emplacement emplacement) {
		this.emplacement = emplacement;
	}



	public Calendar getDate() {
		return date;
	}



	public void setDate(Calendar date) {
		this.date = date;
	}



	public int getNbBillets() {
		return nbBillets;
	}



	public void setNbBillets(int nbBillets) {
		this.nbBillets = nbBillets;
	}



	public int getNbBilletsAchetes() {
		return nbBilletsAchetes;
	}



	public void setNbBilletsAchetes(int nbBilletsAchetes) {
		this.nbBilletsAchetes = nbBilletsAchetes;
	}



	public int getNbBilletsReserves() {
		return nbBilletsReserves;
	}



	public void setNbBilletsReserves(int nbBilletsReserves) {
		this.nbBilletsReserves = nbBilletsReserves;
	}

	public int getNbBilletsDisponibles ( )
	{
		return this.nbBillets - this.nbBilletsAchetes - this.nbBilletsReserves;
	}

	public int getCout() {
		return cout;
	}



	public void setCout(int cout) {
		this.cout = cout;
	}



	public String getTitre() {
		return titre;
	}



	public void setTitre(String titre) {
		this.titre = titre;
	}



	public ArrayList<Billet> getBillets() {
		return billets;
	}



	public void setBillets(ArrayList<Billet> billets) {
		this.billets = billets;
	}



	public synchronized Billet reserverBillet ( ) throws Exception
	{			
		for ( Billet b : billets )
		{				
			if ( b.estDisponible())
			{
				b.reserver ( );
				return b;
			}
		}
			
		if ( nbBillets - nbBilletsReserves - nbBilletsAchetes > 0 )
		{
			Billet billet = new Billet ( this );
			billet.addListener(new Billet.BilletListener()
			{
				@Override
				public void reservationEffectuee(Billet b)
				{
					nbBilletsReserves ++;
				}

				@Override
				public void reservationLiberee(Billet b)
				{
					nbBilletsReserves --;
				}

				@Override
				public void achatEffectue(Billet b)
				{
					nbBilletsAchetes ++;
				}				
			}
			);			
		
			billet.reserver();
			billets.add( billet );
		
			return billet;
		}
		else
		{
			throw new Exception ( "Plus de billets disponibles!" );
		}
	}

}
